package com.example.sys.auth.service;

import com.example.core.po.BaseAuthPo;
import com.example.core.po.BasePo;
import com.example.core.service.BaseCoreServiceImpl;
import com.example.core.utils.UserUtils;
import com.example.core.vo.BaseAuthVo;
import com.example.core.vo.BaseVo;
import com.example.sys.auth.constant.AuthConstant;
import com.example.sys.auth.dao.SysAuthDao;
import com.example.sys.auth.po.SysAuth;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Set;

@Service
public class SysAuthServiceImpl extends BaseCoreServiceImpl<SysAuthDao, SysAuth> implements SysAuthService {

    @Override
    public void save(BasePo po) throws Exception {
        if (po instanceof BaseAuthPo) {
            if (!((BaseAuthPo) po).getSysReaderIds().isEmpty()) {
                Set<String> sysReaderIds = ((BaseAuthPo) po).getSysReaderIds();
                for (String id : sysReaderIds) {
                    SysAuth sysAuth = new SysAuth();
                    sysAuth.setElementId(UserUtils.getUserId());
                    sysAuth.setElementName(UserUtils.getUserName());
                    sysAuth.setType(AuthConstant.READER);
                    sysAuth.setModelId(po.getId());
                    sysAuth.setModelName(po.getClass().getName());
                    sysAuth.setCreateTime(new Date());
                    sysAuth.setUpdateTime(new Date());
                    getBaseMapper().insert(sysAuth);
                }
            }
        }
    }

    @Override
    public void update(BasePo po) throws Exception {
        if (po instanceof BaseAuthPo) {

        }
    }

    @Override
    public void delete(BasePo po) throws Exception {
        if (po instanceof BaseAuthPo) {
            getBaseMapper().deleteByModelId(po.getId());
        }
    }

    @Override
    public void toPo(BasePo po, BaseVo vo) throws Exception {

    }

    @Override
    public void toVo(BasePo po, BaseVo vo) throws Exception {
        if (po instanceof BaseAuthPo && vo instanceof BaseAuthVo) {
            List<SysAuth> sysAuths = getBaseMapper().findList(AuthConstant.READER, po.getClass().getName(), po.getId());
            for (SysAuth sysAuth : sysAuths) {
                ((BaseAuthVo) vo).getSysReaderIds().add(sysAuth.getElementId());
                ((BaseAuthVo) vo).getSysReaderNames().add(sysAuth.getElementName());
            }
        }
    }

    /***
     * 根据用户的权限id查询模块下所有的
     * @param type
     * @param modelName
     * @param authIds
     * @return
     */
    @Override
    public List<String> findList(String type, String modelName, List<String> authIds) {
        return getBaseMapper().findModelIdList(type, modelName, authIds);
    }


    /****
     * 判断用户对modleId 有权限标识
     * @param type
     * @param modelName
     * @param modelId
     * @param authIds
     * @return
     */
    @Override
    public boolean exist(String type, String modelName, String modelId, List<String> authIds) {
        return getBaseMapper().findCount(type, modelName, modelId, authIds) > 0;
    }
}
